package com.scs.application.modules.fsd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.fsd.entity.FsdUsersChestGroup;
import com.scs.application.modules.fsd.mapper.UsersChestGroupMapper;
import com.scs.application.modules.fsd.service.UsersChestGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 */
@Service
public class UsersChestGroupServiceImpl extends BaseServiceImpl<UsersChestGroupMapper, FsdUsersChestGroup> implements UsersChestGroupService {
    @Override
    public List<Map<String,Object>> getChestGroupsByUser(String id) {
        return baseMapper.getChestGroupsByUser(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeChestGroupsByUser(String chestGroupIds, String userId) {
        remove(new QueryWrapper<FsdUsersChestGroup>().eq("user_id",userId));
        Collection<FsdUsersChestGroup> fsdUsersChestGroups = new ArrayList<>();
        String[] chestGroupIdArray = chestGroupIds.split(",");
        for (String chest_Group_id:chestGroupIdArray) {
            if (StringUtils.isBlank(chest_Group_id)) continue;
            FsdUsersChestGroup usersChestGroup = new FsdUsersChestGroup();
            usersChestGroup.setChestGroupId(chest_Group_id);
            usersChestGroup.setUserId(userId);
            fsdUsersChestGroups.add(usersChestGroup);
        }
        saveBatch(fsdUsersChestGroups);
        return true;
    }

    @Override
    public List<Map<String, Object>> getUsersByChestGroup(String id) {
        return baseMapper.getUsersByChestGroup(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changeUsersByChestGroup(String userIds, String chestGroupId) {
        remove(new QueryWrapper<FsdUsersChestGroup>().eq("chest_group_id",chestGroupId));
        Collection<FsdUsersChestGroup> fsdUsersChestGroups = new ArrayList<>();
        String[] userIdArray = userIds.split(",");
        for (String user_id:userIdArray) {
            if (StringUtils.isBlank(user_id)) continue;
            FsdUsersChestGroup usersChestGroup = new FsdUsersChestGroup();
            usersChestGroup.setChestGroupId(chestGroupId);
            usersChestGroup.setUserId(user_id);
            fsdUsersChestGroups.add(usersChestGroup);
        }
        saveBatch(fsdUsersChestGroups);
        return true;
    }
}
